package com.ajim.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("demo")
public class HelloController {
    //http://docmo/hadler01

    @RequestMapping("handler01")
    public ModelAndView handler01() {
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @GetMapping("handler02")
    public ModelAndView handler02(@RequestParam("names") List<String> names) {
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @GetMapping("handler03")
    public ModelAndView handler03(String[] names) {
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @GetMapping("handler04")
    public ModelAndView handler04(Date date) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @GetMapping("handler05")
    @ResponseBody
    public String handler05(Date date) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return "modelAndView";
    }


    /**
     * 重定向的时候使用flash  org.springframework.web.servlet.mvc.support.RedirectAttributes
     * 里面添加一个属性,然后在下个接口上面进行获取
     * ExceptionHandler 统一异常处理,在controller上面只能处理
     */
    @GetMapping("handlerNi")
    @ResponseBody
    public String handlerNi(HttpServletRequest request, String name) {
        String name1 = (String) request.getParameter("name");
        String password = (String) request.getParameter("password");
        return name + "-" + name1 + "-" + password;
    }

}
